/**
 * Description:该类主要用于创建一个富编辑器
 * Copyright: 成都和为科技有限公司 2012
 */
function RichEditor() {}

RichEditor.inherit(Control);

/**
 * 初始化方法
 */
RichEditor.prototype.init = function () {
	RichEditor.base.init.call(this);
}

/**
 * 创建HTML节点
 */
RichEditor.prototype.create = function () {
	//设置编辑器的类型
	if(this.getAttribute().type){
		this._type = this.getAttribute().type;
	}else{
		this._type = "xheditor";
	}
	var gDiv = document.createElement("div");
	var content = document.createElement("textarea");
	$(content).addClass(this._type);
	//设置高度
	var height = this.getAttribute().height;
    if (!height) height = "100";
	if (height.indexOf("px") != -1) {
		height = height.split("px")[0];
	}
	$(content).css(
		{width:"100%",height:parseInt(height) - 30}
	);
	$(gDiv).append(content);
	this._html = gDiv;
}

RichEditor.prototype.initLoad = function() {
	if (!$(this._html).xheditor) {
		this.loadScriptFile("/public.common/webapi/common/xheditor/xheditor-1.2.2.min.js");
		this.loadScriptFile("/public.common/webapi/common/xheditor/xheditor_lang/zh-cn.js");
	}
	$(this._html).xheditor({
		upImgUrl:"/hwsvr2/UploadFileServlet?immediate=1",
		upImgExt:"jpg,jpeg,gif,png",
		upFlashUrl:"/hwsvr2/UploadFileServlet?immediate=1",
		upFlashExt:"swf",
		upMediaUrl:"/hwsvr2/UploadFileServlet?immediate=1",
		upMediaExt:"avi"
	});
}

/**
 * 获取当前控件的值。需要提交到数据库
 */
RichEditor.prototype.getValue = function() {
	if (!this._xheditor) {
		this._xheditor = $('.' + this._type, this.getHTML()).xheditor();
	}
 	var val = this._xheditor.getSource();
	return val;
}

/**
 * 获得错误消息
 */
RichEditor.prototype.getError = function(){
	return this._error;
}

/**
 * 设置错误消息
 */
RichEditor.prototype.setError = function(sError){
	this._error = sError;
}

/**
 * 显示错误
 */
RichEditor.prototype.showError = function() {
	if (!this.isValidate()) {
		WinUtil.showTip({html:this.getError(),
			control:this.getHTML(),
			close:true,time:3000});
	}
}
/**
 * 设置当前控件的值。
 */
RichEditor.prototype.setValue = function(value){
	if (!this._xheditor) {
		this._xheditor = $('.' + this._type, this.getHTML())[0].xheditor;
	}
	this._xheditor.setSource(value);
}
